// cf-542e
// 题意：
//
// 题解：
// 对于每个联通分块，取两个点之间最短路的最大值，所有联通分块的这个
// 值的和就是答案。
//
// ml:run = $bin < input
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

int const maxn = 1007;
int const  inf = 1 << 28;
int father[maxn];
bool color[maxn];
bool vis[maxn];
int ans[maxn];
int dist[maxn][maxn];
int n, m;
std::vector<std::vector<int>> g;

void add_edge(int u, int v)
{
    g[u].push_back(v);
    g[v].push_back(u);
}

bool odd_circle(int u, int f, bool co = false)
{
    vis[u] = true; color[u] = co;
    for (auto v : g[u]) {
        if (v == f) continue;
        if (vis[v]) {
            if (color[v] == color[u]) return true;
        } else {
            if (odd_circle(v, u, co ^ 1)) return true;
        }
    }
    return false;
}

void init()
{
    g.resize(n + 1);
    for (int i = 1; i <= n; i++) father[i] = i;
}

int get_father(int x)
{
    return x == father[x] ? x : father[x] = get_father(father[x]);
}

void set_union(int u, int v)
{
    int tu = get_father(u);
    int tv = get_father(v);
    if (tu != tv) father[tu] = tv;
}

void dijkstra(int source, int dist[])
{
	int num = n + 4;
	std::fill(dist, dist + num, inf);
	dist[source] = 0;
	typedef std::pair<int, int> cost_end;
	std::priority_queue<cost_end, std::vector<cost_end>, std::greater<cost_end> > pq;
	pq.push(std::make_pair(0, source));
	while (!pq.empty()) {
		cost_end tmp = pq.top(); pq.pop();
		int u = tmp.second;
		if (dist[u] < tmp.first) continue;

        for (auto v : g[u]) {
            int c = 1;
			if (dist[v] > dist[u] + c) {
				dist[v] = dist[u] + c;
				pq.push(std::make_pair(dist[v], v));
			}
		}
	}
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin >> n >> m;
    init();
    for (int i = 0; i < m; i++) {
        int u, v; std::cin >> u >> v;
        add_edge(u, v);
        set_union(u, v);
    }

    for (int i = 1; i <= n; i++) father[i] = get_father(father[i]);

    for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        if (odd_circle(i, -1)) { std::cout << "-1\n"; return 0; }
    }

    for (int i = 1; i <= n; i++)
        dijkstra(i, dist[i]);

    std::memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (father[i] != father[j]) continue;
            if (dist[i][j] >= inf) continue;
            int id = father[i];
            ans[id] = std::max(ans[id], dist[i][j]);
        }
    }
    int out = 0;
    for (int i = 1; i <= n; i++) {
        if (vis[father[i]]) continue;
        out += ans[father[i]];
        vis[father[i]] = true;
    }
    std::cout << out << "\n";
}

